import {
  getAllDynamicApi
} from "../../../https/dynamic";
import {
  BASE_URL
} from '../../../constant/index'
const app = getApp();
import {
  getActiveRankingApi
} from '../../../https/user'
import {
  getStoreBannerApi
} from '../../../https/store'
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    BASE_URL,
    // 顶部标题栏动态设置
    topBarInfo: {
      navBarHeight: app.globalData.navBarHeight,
      menuRight: app.globalData.menuRight,
      menuTop: app.globalData.menuTop,
      menuHeight: app.globalData.menuHeight,
    },
    bannerList: [],
    current: 0,
    classifyList: ['发现', '关注', '视频', '图文', ], // 分类标签
    classifyIndex: 0,
    dynamicList: [],
    page: 1,
    limit: 10,
    total: 100,
    isHas: true,
    isLoading: false,
    playingVideoId: null, // 记录当前播放的视频ID
    activeRankingList: [],
    showMore: false,
    changeDocid: "",
    changeUserid: ""
  },
  onLoad() {
    this.getAllDynamic()
    this.getActiveRanking()
    this.getBanner()
  },
  // 获取banner
  async getBanner() {
    const getRes = await getStoreBannerApi();
    console.log(getRes)
    if (getRes.code == 200) {
      const banner = getRes.data.filter(item => item.isShow)
      this.setData({
        bannerList: banner
      })
    }
  },
  // 获取活跃排行榜 
  async getActiveRanking() {
    const getRes = await getActiveRankingApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        activeRankingList: getRes.data
      })
    }
  },

  // 控制播放的视频
  handlePlay(e) {
    const currentVideoId = e.currentTarget.id;

    // 如果有正在播放的视频，并且不是当前视频，则暂停它
    if (this.data.playingVideoId && this.data.playingVideoId !== currentVideoId) {
      const lastVideoContext = wx.createVideoContext(this.data.playingVideoId, this);
      lastVideoContext.pause();
    }

    // 记录当前播放的视频ID
    this.setData({
      playingVideoId: currentVideoId
    });
  },

  // 获取动态数据
  async getAllDynamic() {
    const {
      total,
      page,
      limit,
      isLoading,
      dynamicList,
      isHas
    } = this.data
    console.log('发起请求1')
    console.log(page, limit, total, isLoading)
    if (isLoading) return
    if (!isHas) return
    this.setData({
      isLoading: true
    })
    console.log('发起请求2')
    const getRes = await getAllDynamicApi({
      page,
      limit
    })
    console.log(getRes)

    if (getRes.code == 200) {
      const newDynamicList = [...dynamicList, ...getRes.data.list]
      this.setData({
        dynamicList: newDynamicList,
        page: getRes.data.page + 1,
        total: getRes.data.total
      })
      if ((page) * limit >= getRes.data.total) {
        this.setData({
          isHas: false
        })
      } else {
        this.setData({
          isHas: true
        })
      }

    }
    console.log(this.data)
    this.setData({
      isLoading: false
    })

  },
  // 滚动到底部加载更多
  loadMore(e) {
    console.log('滚动到底部了')
    this.getAllDynamic()
  },
  // 切换轮播图
  toggleSwiper(e) {
    console.log(e)
    const index = e.currentTarget.dataset.index
    this.setData({
      classifyIndex: index
    });
  },
  // 最外部轮播图切换
  swiperChange(e) {
    this.setData({
      classifyIndex: e.detail.current
    });
  },
  // banner轮播图
  bannerSwiperChange: function (e) {
    this.setData({
      current: e.detail.current
    });
  },
  // 打开更多
  openMore(e) {
    const docid = e.currentTarget.dataset.docid
    const userid = e.currentTarget.dataset.userid
    this.setData({
      showMore: true,
      changeDocid: docid,
      changeUserid: userid
    })
  },
  // 关闭弹框
  closePopup() {
    this.setData({
      showMore: false,
    })
  },
  // 跳转详情页面
  gotoDetail(e) {
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../detail/index?docid=' + docid,
    })
  },
  // 跳转用户详情
  gotoMyDetail(e) {
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../../my/detail/index?docid=' + docid,
    })
  },
  // 跳转话题圈
  gotoTopic(e) {
    wx.navigateTo({
      url: '../topic/index',
    })
  },
  // 跳转活动
  gotoActive(e) {
    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '../active/index',
    })
  },
  // 跳转约骑
  gotoOrganization(e) {
    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '../../organization/index/index',
    })
  },
  // 跳转交友
  gotoFriend(e) {
    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '../friend/index',
    })
  },
  // 跳转举报界面
  gotoReport(e) {

    const docid = this.data.changeDocid

    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      this.setData({
        showMore: false
      })
      return
    }
    if (!docid) {
      console.log("没有传入id")
      return
    }

    wx.navigateTo({
      url: '../../my/report/index?docid=' + docid,
    })
    this.setData({
      showMore: false
    })
  },
  // 跳转私聊界面
  gotoChat(e) {
    // const docid = e.currentTarget.dataset.docid
    const userId = this.data.changeUserid

    const user = wx.getStorageSync('userData')
    console.log(user)
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      this.setData({
        showMore: false
      })
      return
    }
    if (userId == user.id) {
      wx.showToast({
        title: '不能与自己私聊',
        icon: 'none'
      })
      this.setData({
        showMore: false
      })
      return
    }
    // return

    wx.navigateTo({
      url: '../../message/chat/index?docid=' + userId,
    })
    this.setData({
      showMore: false
    })
  },
  // 跳转购物界面
  gotoStore(e) {
    console.log(1)
    wx.switchTab({
      url: '../../store/index/index',
    })
  },
  // 跳转banner详情界面
  gotoStoreDetail(e) {
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../../store/detail/index?docid=' + docid,
    })
  }

})